package com.zhiwang.planet.ui.my

import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerUserComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.mvp.p.BindPresenter
import com.zhiwang.planet.mvp.p.GraphCodePresenter
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.ChangePayPasswordView
import com.zhiwang.planet.mvp.v.GraphView
import com.zhiwang.planet.mvp.v.SmsCodeView
import com.zhiwang.planet.mvp.v.UserInfoView
import com.zhiwang.planet.mvvm.model.entity.User
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.suicideAfterTransition
import kotlinx.android.synthetic.main.activity_bind_pay_password.*
import java.util.*
import javax.inject.Inject

class BindPayPasswordActivity : BaseActivity(), UserInfoView, GraphView, SmsCodeView, ChangePayPasswordView {

    @Inject
    lateinit var userPresenter: UserPresenter

    @Inject
    lateinit var graphPresenter: GraphCodePresenter

    @Inject
    lateinit var bindPresenter: BindPresenter

    var seed = Random().nextLong().toString()

    lateinit var phone: String

    lateinit var userInfo: User

    @Inject
    lateinit var userDao: UserDao

    override fun initInjection() {
        super.initInjection()
        DaggerUserComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)

        userPresenter.userInfoView = this
        bindPresenter.smsCodeView = this
        userPresenter.smsCodeView = this
        graphPresenter.graphView = this
        userPresenter.changePayPasswordView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bind_pay_password)
        setSupportActionBar(toolbar)
        observeTextChangeWithTargetButtonEnableStatus(
                arrayOf(smsCodeEditor, payPasswordConfirmEditor, payPasswordEditor),
                confirmBindBtn)
        userPresenter.queryUserInfo()
        userPresenter.loadUserInfoEx({
//            tel.text = it.phone
        },{

        })
        getGraphCode(graphCodeBtn)
    }

    //获取修改密码验证码
    fun getSmsCode(v: View) {
//        val phone = tel.text.toString()
        val graphCode = graphCodeEditor.text.toString()
        if (TextUtils.isEmpty(graphCode)) {
            Toaster.message("图形验证码为空")
            return
        }

        bindPresenter.getBindSmsCode(phone, seed, graphCode)
    }

    override fun onSmsSend() {
        Toaster.message(R.string.message_sms_send)
        captchaTextView.start()
    }

    fun getGraphCode(v: View) {
        seed = System.currentTimeMillis().toString()
        graphPresenter.getBindGraphCode(seed)
    }

    override fun onGraphCodeLoad(b: Bitmap) {
        graphCodeBtn.setImageBitmap(b)
    }

    override fun onPayPwdChanged() {
        Toaster.message("绑定密码成功")
        suicideAfterTransition()
    }

    fun confirmBindPayPassword(v: View) {
        val smsCode = smsCodeEditor.text.toString()
        val payPassword = payPasswordEditor.text.toString()
        val payPasswordConfirm = payPasswordConfirmEditor.text.toString()
        if (smsCode.isEmpty()) {
            Toaster.message(R.string.captcha_empty)
            return
        } else if (!TextUtils.equals(payPassword, payPasswordConfirm)) {
            Toaster.message(R.string.pay_password_different)
            return
        }
        userPresenter.bindPayPassword(payPassword, smsCode)
    }


    override fun onUserInfoLoad(user: User?) {
        if (user != null) {
            this.userInfo = user
            if (userInfo.bindInfo.hasSetPaypassword)
                toolbar.title = "修改交易密码"
            else toolbar.title = "绑定交易密码"
            phone = user.phone
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return super.onSupportNavigateUp()
    }


}
